import 'package:flutter/material.dart';
import 'package:flutter_base/page/home/home_main_page.dart';
import 'package:flutter_base/utils/navigator_utils.dart';
import 'package:flutter_base/utils/sp_utils.dart';
import 'package:flutter_base/utils/toast_utils.dart';

import 'common/sp_key.dart';

///
/// @DIR_PATH：lib
/// @TIME：2023/11/11 9:29
/// @AUTHOR：starr
/// @DESC：引导页面 第一次进入展示的页面
///
class SplashPage extends StatefulWidget {
  const SplashPage({super.key});

  @override
  _IndexPageState createState() => _IndexPageState();
}

class _IndexPageState extends State<SplashPage> {
  // lib/src/splash_page.dart
  //构建的右上角显示的计数数据
  String text1 = "1";
  String text2 = "";

  //右上角显示计数的容大小
  double topWidth = 40;
  double topHeight = 40;

  //右上角显示计数的边框圆角
  //默认容器的大小为40，这里设置的边框圆角为 20
  //所以展示出来的是一个圆形
  double topRadius = 20;

  //是否显示当前时间是第几周
  //因为只有在最后一页才显示的
  bool isShowWeek = false;

  //引导页面使用的图片资源
  final List<String> _splList = [
    "assets/images/3.0x/sp01.png",
    "assets/images/3.0x/sp02.png",
    "assets/images/3.0x/sp03.png",
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //填充布局
      body: Container(
        width: double.infinity,
        height: double.infinity,
        child: Stack(
          alignment: Alignment.center,
          children: [
            //底部可滑动的图片
            buildPageView(),
            //右上角显示的计数
            // buildTopDate(),
            //左下角显示的进入首页面按钮
            buildBottomButton(),
          ],
        ),
      ),
    );
  }

  // lib/src/splash_page.dart
  // 底部可滑动的图片
  Widget buildPageView() {
    //PageView用于整屏切换效果
    //默认情况下是左右切换
    return PageView.builder(
      //滑动视频滑动结束时回调
      //参数 value PageView当前显示的页面索引
      onPageChanged: (value) {
        ToastUtils.showToast("pageView on changed $value");
        if (value < _splList.length - 1) {
          isShowWeek = false;
        } else {
          isShowWeek = true;
        }
        setState(() {});
        //修改右上角的页面角标
        // buildTopText(value);
      },
      //构建条目的总个数，如这里的3
      itemCount: _splList.length,
      //每一页的显示Widget
      itemBuilder: (BuildContext context, int postion) {
        //这里直接使用的是本地资源目录下的图片
        return Image.asset(
          _splList[postion],
          fit: BoxFit.fill,
        );
      },
    );
  }

  Widget buildBottomButton() {
    if (isShowWeek) {
      return Positioned(
        bottom: 40,
        left: 20,
        child: InkWell(
          onTap: () {
            //保存标识
            SPUtils.save(spUserIsFirstKey, true);
            //跳转首页
            NavigatorUtils.openPageByFade(context, const HomeMainPage(),
                isReplace: true);
          },
          child: Container(
            //内边距
            padding:
                const EdgeInsets.only(left: 10, right: 10, top: 4, bottom: 4),
            alignment: Alignment.center,
            //圆角边框
            decoration: BoxDecoration(
                borderRadius: const BorderRadius.all(Radius.circular(20)),
                border: Border.all(
                  width: 3,
                  color: Colors.indigo,
                )),
            child: const Text(
              "去首页",
              maxLines: 1,
              overflow: TextOverflow.clip,
            ),
          ),
        ),
      );
    } else {
      return Container();
    }
  }
}
